//
//  photoViewController.m
//  综合练习
//
//  Created by rimi on 16/5/14.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "photoViewController.h"
#import "SDAutoLayout.h"
#import "AVFile.h"
#import "BaseRequest+MoneyAndRemember.h"
#import "UserModel.h"
@interface photoViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation photoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.1216 green:0.1216 blue:0.1765 alpha:1.0];
    
    _imageView = [[UIImageView alloc]init];
    _imageView.image = _image;
    _imageView.backgroundColor = [UIColor whiteColor];
    _imageView.layer.cornerRadius = self.view.frame.size.width*0.4/2.0;
    _imageView.layer.masksToBounds = YES;
    [self.view addSubview:_imageView];
    _imageView.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .topSpaceToView(self.view,self.view.frame.size.width*0.4)
    .widthIs(self.view.frame.size.width*0.4)
    .heightEqualToWidth();
    
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeButton.layer.cornerRadius  = 10;
    [changeButton setTitle:@"更换头像" forState:UIControlStateNormal];
    changeButton.layer.borderColor = [UIColor colorWithRed:0.8275 green:0.7961 blue:0.7216 alpha:1.0].CGColor;
    changeButton.layer.borderWidth = 1;
    [changeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    changeButton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .bottomSpaceToView(self.view,self.view.frame.size.width*0.4)
    .widthIs(self.view.frame.size.width*0.3)
    .heightIs(self.view.frame.size.width*0.1);
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.layer.cornerRadius  = 10;
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    sureButton.layer.borderColor = [UIColor colorWithRed:0.8275 green:0.7961 blue:0.7216 alpha:1.0].CGColor;
    sureButton.layer.borderWidth = 1;
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureButton];
    sureButton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .bottomSpaceToView(self.view,self.view.frame.size.width*0.25)
    .widthIs(self.view.frame.size.width*0.3)
    .heightIs(self.view.frame.size.width*0.1);
    
}

- (void)selectAlbum
{
    UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
    imagepicker.delegate = self;
    //资源类型
    imagepicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagepicker.allowsEditing = YES;
    [self presentViewController:imagepicker animated:YES completion:nil];
}

- (void)takePhoto
{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"设备不支持！" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        [self performSelector:@selector(delayDismiss) withObject:nil afterDelay:1];
        return ;
    }
    UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
    imagepicker.delegate = self;
    imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagepicker.allowsEditing = YES;
    [self presentViewController:imagepicker animated:YES completion:nil];
}

- (void)delayDismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)changeAction:(UIButton *)sender
{
    UIAlertController *photo = [UIAlertController alertControllerWithTitle:@"上传头像" message:@"选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    //防止循环引用
    __weak typeof(self) weakSelf = self;
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf selectAlbum];
    }];
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf takePhoto];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [photo addAction:albumAction];
    [photo addAction:takePhotoAction];
    [photo addAction:cancel];
    [self presentViewController:photo animated:YES completion:nil];
}

- (void)sureAction:(UIButton *)sender {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    //3.执行 block
    self.passValueHandle(_imageView.image);
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //    [self.cacenButtonArr addObject:@""];
    NSData* imageData = UIImageJPEGRepresentation(info[@"UIImagePickerControllerEditedImage"], 1.0);
    NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* imagePath = [documentPath stringByAppendingPathComponent:[UserModel shareUserModel].userNmae];
    [imageData writeToFile:imagePath atomically:NO];
    AVFile *file = [AVFile fileWithName:[UserModel shareUserModel].userNmae contentsAtPath:imagePath];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"%@",file.url);
            _imageView.image = info[@"UIImagePickerControllerEditedImage"];
            [picker dismissViewControllerAnimated:YES completion:nil];
            [picker dismissViewControllerAnimated:YES completion:nil];
            [BaseRequest updateHeadImageFromUserName:file.url];
        }else {
            NSLog(@"设备出了问题");
        }
    }];
    
}

@end
